package com.agsmobi.digitapp;

import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


/*
 * NumberTiles
 */
public class NumberTilesActivity extends BaseActivity {

	GridView gridView;
	int numberOfRows;
	int numberOfColumns;
	int positionX;
	int positionY; 
	int width; 
	int height;
	ArrayList<Integer> numbers;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_numbertile);
		loadControl();
		setDemoData();
		show();
	}
	
	@Override
	protected void loadControl() {
		// TODO Auto-generated method stub
		super.loadControl();
		gridView = (GridView)findViewById(R.id.gridView);
	}
	
	// method make demo data for example
	public void setDemoData () {
		setNumberOfRows(2);
		setNumberOfColumns(3);
		int [] numbersArr = new int []{1, 2, 12 ,2, 2, 12};
		numbers = new ArrayList<Integer>();
		for (int i = 0; i < numbersArr.length; i++) {
			numbers.add(numbersArr[i]);
		}
		setPositionX((int)(0.3 * screenWidth));
		setPositionY((int)(0.2 * screenHeight));
		setWidth((int)(0.4 * screenWidth));
		
	}
	
	public void show() {
		// set position, size parent layout of all number tiles
		LayoutParams param = new LayoutParams(width, LayoutParams.WRAP_CONTENT);
		param.setMargins(positionX, positionY, 0, 0);
		gridView.setLayoutParams(param);
		
		//creat and add number tiles
		int tileSize = (int) (width/numberOfColumns);
		setHeight(tileSize*numberOfRows);
		gridView.setNumColumns(numberOfColumns);
		CustomAdapter adapter = new CustomAdapter(numbers, tileSize, this);
		gridView.setAdapter(adapter);
		
		// set click event on tile
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				showMessage("Number clicked is" + String.valueOf(numbers.get(position)), false);
			}
		});
	}

	public int getNumberOfRows() {
		return numberOfRows;
	}

	public void setNumberOfRows(int numberOfRows) {
		this.numberOfRows = numberOfRows;
	}

	public int getNumberOfColumns() {
		return numberOfColumns;
	}

	public void setNumberOfColumns(int numberOfColumns) {
		this.numberOfColumns = numberOfColumns;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public ArrayList<Integer> getNumbers() {
		return numbers;
	}

	public void setNumbers(ArrayList<Integer> numbers) {
		this.numbers = numbers;
	}
	
	
	// adapter to view tile into the grid
	class CustomAdapter extends BaseAdapter {

		Context mContenxt;
		ArrayList<Integer> mData;
		int mTileSize;
		
		public CustomAdapter (ArrayList<Integer> data, int sizeOfTile, Context context) {
			mContenxt = context;
			mData = data;
			mTileSize = sizeOfTile;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mData.get(position);
		}

		@Override
		public long getItemId(int id) {
			// TODO Auto-generated method stub
			return id;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = (LayoutInflater) mContenxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.row_numbertile, parent, false);
			
			TextView numTile = (TextView)rowview.findViewById(R.id.numTile);
			android.widget.LinearLayout.LayoutParams param = new android.widget.LinearLayout.LayoutParams(mTileSize, mTileSize);
			param.setMargins(0, 0, 0, 0);
			numTile.setLayoutParams(param);
			numTile.setBackgroundResource(BaseActivity.imageDigit[mData.get(position)]);
			
			return rowview;
		}
		
	}
	

}
